class CategorysController {

    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        params.sort = 'sequencer'
        params.order = 'asc'
        if (params.id && params.id != "") {
            def parent = Categorys.get(params.id)
             ['parent' : parent ,
             categorysInstanceList: Categorys.findAllBySchoolAndParent(session.school ,parent ,params),
             categorysInstanceTotal: Categorys.countBySchoolAndParent(session.school ,parent)]
        }else{
            [categorysInstanceList: Categorys.findAllBySchoolAndParentIsNull(session.school , params), 
            categorysInstanceTotal: Categorys.countBySchoolAndParentIsNull(session.school)]
        }
    }

    def create = {
        def categorysInstance = new Categorys()
//        categorysInstance.properties = params
        if (params.id) {
            categorysInstance.parent = Categorys.get(params.id)
        }
        return [categorysInstance: categorysInstance]
    }

    def save = {
        def categorysInstance = new Categorys(params)
        categorysInstance.school = session.school

        if (categorysInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'categorys.label', default: 'Categorys'), categorysInstance])}"
            redirect(action: "list"  )
        }
        else {
            render(view: "create", model: [categorysInstance: categorysInstance])
        }
    }

    def show = {
        def categorysInstance = Categorys.get(params.id)
        if (!categorysInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'categorys.label', default: 'Categorys'), params.id])}"
            redirect(action: "list")
        }
        else {
            [categorysInstance: categorysInstance]
        }
    }

    def edit = {
        def categorysInstance = Categorys.get(params.id)
        if (!categorysInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'categorys.label', default: 'Categorys'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [categorysInstance: categorysInstance]
        }
    }

    def update = {
        def categorysInstance = Categorys.get(params.id)
        if (categorysInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (categorysInstance.version > version) {
                    
                    categorysInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'categorys.label', default: 'Categorys')] as Object[], "Another user has updated this Categorys while you were editing")
                    render(view: "edit", model: [categorysInstance: categorysInstance])
                    return
                }
            }
            categorysInstance.properties = params
            if (!categorysInstance.hasErrors() && categorysInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'categorys.label', default: 'Categorys'), categorysInstance])}"
                redirect(action: "list"   )
            }
            else {
                render(view: "edit", model: [categorysInstance: categorysInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'categorys.label', default: 'Categorys'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def categorysInstance = Categorys.get(params.id)
        if (categorysInstance) {
            if (categorysInstance?.children) {
                log.info "categorys.delete children is extis ${categorysInstance?.children}"
                flash.message = "包含子栏目，不能删除。"
                return redirect(action: "list" )
            }
            try {
                categorysInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'categorys.label', default: 'Categorys'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'categorys.label', default: 'Categorys'), params.id])}"
                redirect(action: "list" )
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'categorys.label', default: 'Categorys'), params.id])}"
            redirect(action: "list"  )
        }
    }
}
